package com.zy.mc_module.ui.login;

import android.content.Intent;
import android.view.WindowManager;
import android.widget.EditText;

import com.zy.mc_module.R;
import com.zy.mc_module.base.App;
import com.zy.mc_module.base.BaseActivity;
import com.zy.mc_module.common.Constant;
import com.zy.mc_module.mvp.bean.UserBean;
import com.zy.mc_module.mvp.contract.SetPwdContract;
import com.zy.mc_module.mvp.contract.SetPwdContract.View;
import com.zy.mc_module.mvp.presenter.SetPwdPresenter;
import com.zy.mc_module.ui.setting.AccountActivity;
import com.zy.mc_module.utils.AppTools;
import com.zy.mc_module.utils.GsonUtil;
import com.zy.mc_module.utils.SPUtil;
import com.zy.mc_module.utils.StatusBarUtil;
import com.zy.mc_module.utils.ToastUtil;
import com.zy.mc_module.utils.UserUtils;
import com.zy.mc_module.view.CustomHeadLayout;
import com.zy.mc_module.utils.retrofit.error.ThdException;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * Created on 18/12/1
 * 新用户设置密码
 */
public class SetPwdActivity extends BaseActivity<SetPwdContract.View, SetPwdPresenter> implements View {
    @BindView(R.id.customhead)
    CustomHeadLayout customhead;
    @BindView(R.id.setpwd_et_userName)
    EditText et_UserName;
    @BindView(R.id.setpwd_et_repwd)
    EditText et_repwd;
    @BindView(R.id.setpwd_et_pwd)
    EditText et_pwd;
    @BindView(R.id.setpwd_et_incode)
    EditText et_incode;

    //密码 用户名 邀请码 手机号 验证码 openid unionid 微信昵称 微信头像
    private String pwd, repwd, userName, incode, tel, code, openid, unionid, wxname, wxhead;
    private boolean flagSetPassword;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setpwd;
    }

    @Override
    protected boolean isNeedRxBus() {
        return true;
    }

    @Override
    protected SetPwdPresenter createPresenter() {
        return new SetPwdPresenter();
    }

    @Override
    protected void initView() {
        App.getInstance().addTmpActivity(this);
        StatusBarUtil.setImmersiveStatusBar(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        customhead.setLeftClickListener(view -> finish());
        flagSetPassword = getIntent().getBooleanExtra("flagSetPassword", false);
        if (flagSetPassword) {
            et_incode.setEnabled(false);
            //et_UserName.setText(UserUtils.getUserName());
        }
    }

    /***
     * 显示密码
     */
    @OnCheckedChanged(R.id.setpwd_show1)
    public void show1(boolean isChecked) {
        AppTools.hidePwd(et_pwd, isChecked);
    }

    /***
     * 显示确认密码
     */
    @OnCheckedChanged(R.id.setpwd_show2)
    public void show2(boolean isChecked) {
        AppTools.hidePwd(et_repwd, isChecked);
    }

    /***
     * 下一步 完善个人资料
     */
    @OnClick(R.id.setpwd_sure)
    public void sure() {
        if (flagSetPassword) {
            userName = et_UserName.getText().toString().trim();
            pwd = et_pwd.getText().toString().trim().toLowerCase();
            repwd = et_repwd.getText().toString().trim().toLowerCase();
            presenter.H5SetPassword(context, userName, pwd, repwd);
        } else {
            userName = et_UserName.getText().toString().trim();
            incode = et_incode.getText().toString().trim();
            pwd = et_pwd.getText().toString().trim().toLowerCase();
            repwd = et_repwd.getText().toString().trim().toLowerCase();
            tel = getIntent().getStringExtra("tel");
            code = getIntent().getStringExtra("code");
            openid = AppTools.getHasKeyS(this, "openid");
            unionid = AppTools.getHasKeyS(this, "unionid");
            wxname = AppTools.getHasKeyS(this, "wxname");
            wxhead = AppTools.getHasKeyS(this, "wxhead");
            //设置密码
            presenter.setPwd(context, userName, pwd, repwd, tel, code, incode.toUpperCase(), openid, unionid, wxname, wxhead);
        }
    }

    /***
     * 设置密码成功,登录成功,保存用户信息
     */
    @Override
    public void onSetPwdSuccess(UserBean userBean) {

        Map<String, Object> map = new HashMap<>();
        SPUtil.putString(Constant.USER_H5_KEY, GsonUtil.gsonString(map));
        UserUtils.saveUser(userBean);
        Intent intent = new Intent(context, AccountActivity.class);
        intent.putExtra("type", 0);
        startActivity(intent);
        App.getInstance().finishTmpActivity();
    }

    @Override
    public <T> void onSuccess(T t, int type) {
        if (type == 0) {
            Boolean aBoolean = (Boolean) t;
            if (aBoolean) {
                Intent intent = new Intent(context, AccountActivity.class);
                intent.putExtra("type", 0);
                startActivity(intent);
                App.getInstance().finishActivity(LoginActivity.class);
                App.getInstance().finishTmpActivity();
            }
        }
    }

    @Override
    public void onError(ThdException error) {
        ToastUtil.showShort(error.message);
    }
}
